package com.exsun.ex1077.ui.activity.modifypwd

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.blankj.utilcode.util.EncryptUtils
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.network.apiService
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.BooleanObservableField
import com.zhangping.fastjetpack.extend.databind.StringObservableField

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/11 10:46
 */
class ModifyPwdViewModel: BaseViewModel() {

    val oldPwd = StringObservableField()
    val newPwd = StringObservableField()
    val confirmPwd = StringObservableField()

    val showOldPwd = BooleanObservableField()
    val showNewPwd = BooleanObservableField()
    val showConfirmPwd = BooleanObservableField()

    val submitResult = MutableLiveData<ResultState<Any?>>()

    fun submitModify() {
        request({ apiService.modifyPassword(JSONObject().apply {
            put("oldPassword", getAesPassword(oldPwd.get().trim()))
            put("newPassword", getAesPassword(newPwd.get().trim()))
        })}, submitResult, true)
    }

    private fun getAesPassword(password: String): String{
        return EncryptUtils.encryptAES2HexString(password.toByteArray(),
            Constant.PASSWORD_AES_SECRET.toByteArray(), "AES/ECB/PKCS5Padding", null)
    }
}